package com.x.erp.dao;

import com.x.erp.model.Cooperation;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 启辕合作信息dao
 * 
 */
@Repository
public class CooperationDao extends BaseDao {

	private final static String selectCooperationListSql = "SELECT T1.* FROM ERP_COOPERATION T1 WHERE 1=1 " ;

	/**
	 * 查询启辕合作信息列表
	 * 
	 * @param cooperation
	 * @return
	 * @throws Exception
	 */
	public List<Cooperation> selectCooperationList(Cooperation cooperation) throws Exception {
		StringBuilder sql = new StringBuilder(selectCooperationListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, cooperation);
		sql.append("ORDER BY T1.COOPERATION_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), Cooperation.class, params, cooperation);
	}

	private final static String selectCooperationNumSql = "SELECT COUNT(T1.COOPERATION_ID) FROM ERP_COOPERATION T1 WHERE 1=1 " ;

	/**
	 * 查询启辕合作信息数量
	 *
	 * @param cooperation
	 * @return
	 * @throws Exception
	 */
	public Integer selectCooperationNum(Cooperation cooperation) throws Exception {
		StringBuilder sql = new StringBuilder(selectCooperationNumSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, cooperation);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * 
	 * @param sql
	 * @param params
	 * @param cooperation
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, Cooperation cooperation) {
		if (cooperation != null) {
			if (StringUtils.isNotEmpty(cooperation.getCooperationId())) {
				sql.append("AND T1.COOPERATION_ID = ? ");
				params.add(cooperation.getCooperationId());
			}
			if (StringUtils.isNotEmpty(cooperation.getCooperationMobile())) {
				sql.append("AND T1.COOPERATION_MOBILE = ? ");
				params.add(cooperation.getCooperationMobile());
			}
			if (cooperation.getCooperationCreateTime() != null) {
				sql.append("AND T1.COOPERATION_CREATE_TIME = ? ");
				params.add(cooperation.getCooperationCreateTime());
			}
			if (StringUtils.isNotEmpty(cooperation.getCooperationSource())) {
				sql.append("AND T1.COOPERATION_SOURCE = ? ");
				params.add(cooperation.getCooperationSource());
			}
			if (StringUtils.isNotEmpty(cooperation.getCooperationName())) {
				sql.append("AND T1.COOPERATION_NAME = ? ");
				params.add(cooperation.getCooperationName());
			}
			if (StringUtils.isNotEmpty(cooperation.getCooperationCompany())) {
				sql.append("AND T1.COOPERATION_COMPANY = ? ");
				params.add(cooperation.getCooperationCompany());
			}
			if (StringUtils.isNotEmpty(cooperation.getCooperationRemark())) {
				sql.append("AND T1.COOPERATION_REMARK = ? ");
				params.add(cooperation.getCooperationRemark());
			}
			if (cooperation.getCooperationState() != null) {
				sql.append("AND T1.COOPERATION_STATE = ? ");
				params.add(cooperation.getCooperationState());
			}
		}
	}
}
